//
//  LRComposePicCollectionV.swift
//  WeiboX
//
//  Created by admin on 11/12/2016.
//  Copyright © 2016 iOS_Objective-C. All rights reserved.
//

import UIKit

class LRComposePicCollectionV: UICollectionView {
	
	var callBack: (() -> ())?
	
	fileprivate let LRComposePicCellID: String = "LRComposePicCellID"
	
	lazy var dataArr: [UIImage] = [UIImage]()

	override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
		let flowLayout = UICollectionViewFlowLayout()
		
		let itemMargin: CGFloat = 5
		let itemWidth = ((ScreenWidth - 20) - 2 * itemMargin) / 3
		flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
		
		flowLayout.minimumLineSpacing = itemMargin
		flowLayout.minimumInteritemSpacing = itemMargin
		
		super.init(frame: frame, collectionViewLayout: flowLayout)
		
		setupUI()
	}
	
	private func setupUI() {
		register(LRComposePicCell.self, forCellWithReuseIdentifier: LRComposePicCellID)
		
		dataSource = self
		delegate = self
		
		isHidden = true
	}
	
	func addImage(img: UIImage) {
		if dataArr.count > 8 {
			return
		} else {
			isHidden = false	// 显示配图
			
			dataArr.append(img)
			
			reloadData()
		}
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}

}

// MARK: - 数据源和代理方法
extension LRComposePicCollectionV: UICollectionViewDataSource, UICollectionViewDelegate {
	internal func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
		if dataArr.count == 0 || dataArr.count == 9 {
			return dataArr.count
		} else {
			return dataArr.count + 1
		}
	}
	
	internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
		let cell = dequeueReusableCell(withReuseIdentifier: LRComposePicCellID, for: indexPath) as! LRComposePicCell
		
		print(indexPath.row)
		if indexPath.item == dataArr.count {
			cell.pic = nil
		} else {
			cell.pic = dataArr[indexPath.row]
			cell.indexPath = indexPath
			
			cell.deleteCallBack = { (index) -> Void in
				self.dataArr.remove(at: index)
				
				if self.dataArr.count == 0 {
					collectionView.isHidden = true
				}
				collectionView.reloadData()
			}
		}
		return cell
	}
	
	internal func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
		if indexPath.item == dataArr.count {	// 点击最后一个 cell
			callBack?()
		}
	}
}

class LRComposePicCell: UICollectionViewCell {
	
	var deleteCallBack: ((Int) -> ())?
	
	var indexPath: IndexPath?
	
	private lazy var picImgV: UIImageView = UIImageView()
	
	private lazy var deleteBtn: UIButton = {
		let btn = UIButton()
		btn.setImage(#imageLiteral(resourceName: "compose_photo_close"), for: .normal)
		btn.addTarget(self, action: #selector(deleteBtnAction), for: .touchUpInside)
		return btn
	}()
	
	fileprivate var pic: UIImage? {
		didSet {
			if pic == nil {
				picImgV.image = #imageLiteral(resourceName: "compose_pic_add")
				deleteBtn.isHidden = true
			} else {
				picImgV.image = pic
				deleteBtn.isHidden = false
			}
		}
	}
	
	@objc private func deleteBtnAction() {
		if let currentIndexPath = indexPath {
			deleteCallBack?(currentIndexPath.item)
		}
	}
	
	override init(frame: CGRect) {
		super.init(frame: frame)
		setupUI()
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
	private func setupUI() {
		contentView.addSubview(picImgV)
		contentView.addSubview(deleteBtn)
		
		picImgV.snp.makeConstraints { (make) in
			make.edges.equalToSuperview()
		}
		
		deleteBtn.snp.makeConstraints { (make) in
			make.top.trailing.equalTo(picImgV)
		}
	}
}
